<script setup lang="ts">
import type { FormRules } from 'element-plus'
import { onMounted, reactive, ref } from 'vue'
import { dictionaryAll } from '@/api/interface/common/dictionary'

interface Props {
  data?: Record<string, any> | never | null
  status?: string | never | null
}

const { data = null } = defineProps<Props>()

const formRef = ref()

const form = reactive<Record<string, any>>({})

const rules = reactive<FormRules>({
  name: [{ required: true, message: '名称不能为空!', trigger: 'blur' }],
  routename: [{ required: true, message: '路由名称不能为空!', trigger: 'blur' }],
  path: [{ required: true, message: '路由路径不能为空!', trigger: 'blur' }],
  components: [{ required: true, message: '路由组件不能为空!', trigger: 'blur' }],
})

const types: any = ref([])
const dictionaryAllFun = () => {
  dictionaryAll({ type: '1' }).then((res: any) => {
    types.value = res
  })
}
dictionaryAllFun()

const componentsOptions = [
  {
    value: '#',
    label: '系统布局组件',
  },
  {
    value: 'NoComponent',
    label: '无组件',
  },
]

const initForm = () => {
  if (data) {
    Object.keys(data).forEach((key: string) => {
      form[key] = data![key]
    })
  }
}

const handleTagClick = (val: any) => {
  form.components = val
}

const formatForm = () => {
  const result: Record<string, any> = {}
  Object.keys(form).forEach((key: string) => {
    result[key] = form[key]
  })
  return result
}

const submitForm = () => {
  return new Promise<any>((resolve) => {
    formRef.value.validate((valid: boolean) => {
      let res = null
      if (valid) {
        res = formatForm()
      }
      resolve(res)
    })
  })
}

defineExpose({
  submitForm,
})

onMounted(() => {
  initForm()
})
</script>

<template>
  <div class="form-wrapper">
    <BaseForm
      ref="formRef"
      :form="form"
      :rules="rules"
      width="360px"
      :attributes="{ 'label-width': '90px' }"
    >
      <el-form-item label="类型:" prop="type">
        <el-select v-model="form.type" placeholder="请选择类型">
          <el-option
            v-for="item in types"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="名称:" prop="name">
        <el-input v-model="form.name" type="text" placeholder="请输入内容"> </el-input>
      </el-form-item>
      <el-form-item label="路由名称:" prop="routename">
        <el-input v-model="form.routename" type="text" placeholder="请输入内容"> </el-input>
      </el-form-item>
      <el-form-item v-if="form.type != 3" label="路由路径:" prop="path">
        <el-input v-model="form.path" type="text" placeholder="请输入内容"> </el-input>
      </el-form-item>
      <el-form-item v-if="form.type != 3" label="路由组件:" prop="components">
        <el-input v-model="form.components" type="text" placeholder="请输入内容"></el-input>
      </el-form-item>
      <el-form-item v-if="form.type != 3" label="">
        <el-check-tag
          v-for="comp in componentsOptions"
          :key="comp.value"
          style="margin-right: 10px"
          type="info"
          @click="handleTagClick(comp.value)"
        >
          {{ comp.label }}
        </el-check-tag>
      </el-form-item>
      <el-form-item v-if="form.type != 3" label="icon图标:" prop="icon">
        <el-input v-model="form.icon" type="text" placeholder="请输入内容"> </el-input>
      </el-form-item>
      <el-form-item label="状态:" prop="status">
        <el-switch v-model="form.status" active-value="1" inactive-value="0" />
      </el-form-item>
    </BaseForm>
  </div>
</template>

<style lang="scss" scoped></style>
